package com.test.nnooolwdeaer.util_wooden.imageUtil_wooden;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageCompress_wooden {
    public File srcImg_wooden;
    private File tagImg_wooden;
    private int srcWidth_wooden;
    private int srcHeight_wooden;
    private boolean focusAlpha_wooden = false;


    public ImageCompress_wooden(File srcImg, File tagImg, boolean focusAlpha) throws IOException {
        this.srcImg_wooden = srcImg;
        this.tagImg_wooden = tagImg;
        this.focusAlpha_wooden = focusAlpha;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;

        BitmapFactory.decodeStream(new FileInputStream(srcImg), null, options);
        this.srcWidth_wooden = options.outWidth;
        this.srcHeight_wooden = options.outHeight;
    }

    private int computeSize_wooden() {
        srcWidth_wooden = srcWidth_wooden % 2 == 1 ? srcWidth_wooden + 1 : srcWidth_wooden;
        srcHeight_wooden = srcHeight_wooden % 2 == 1 ? srcHeight_wooden + 1 : srcHeight_wooden;

        int longSide = Math.max(srcWidth_wooden, srcHeight_wooden);
        int shortSide = Math.min(srcWidth_wooden, srcHeight_wooden);

        float scale = ((float) shortSide / longSide);
        if (scale <= 1 && scale > 0.5625) {
            if (longSide < 1664) {
                return 1;
            } else if (longSide < 4990) {
                return 2;
            } else if (longSide > 4990 && longSide < 10240) {
                return 4;
            } else {
                return longSide / 1280 == 0 ? 1 : longSide / 1280;
            }
        } else if (scale <= 0.5625 && scale > 0.5) {
            return longSide / 1280 == 0 ? 1 : longSide / 1280;
        } else {
            return (int) Math.ceil(longSide / (1280.0 / scale));
        }
    }

    private Bitmap rotatingImage_wooden(Bitmap bitmap, int angle) {
        Matrix matrix = new Matrix();

        matrix.postRotate(angle);

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    public File compress_wooden() throws IOException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = computeSize_wooden();

        Bitmap tagBitmap = BitmapFactory.decodeStream(new FileInputStream(srcImg_wooden), null, options);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        if (ImageChecker_wooden.SINGLE_wooden.isJPG_wooden(new FileInputStream(srcImg_wooden))) {
            tagBitmap = rotatingImage_wooden(tagBitmap, ImageChecker_wooden.SINGLE_wooden.getOrientation_wooden(new FileInputStream(srcImg_wooden)));
        }

        tagBitmap.compress(focusAlpha_wooden ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 60, stream);
        tagBitmap.recycle();

        FileOutputStream fos = new FileOutputStream(tagImg_wooden);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.close();

        return tagImg_wooden;
    }
}
